//
//  FirstController.swift
//  fho
//
//  Created by 郭颢源 on 2021/3/19.
//

/// 这是一个结构体。 ///
/// 它什么都没干......
/// - important: 这很重要!
/// - warning: 警告!
/// - attention: 注意!
/// - note: 另外......没什么好说的了......
/// - version: 1.0
/// - author: Zenny Chen
// MARK: - foo函数 func foo()

// MARK:  foo函数 func foo()
// TODO: - asdf
// FIXME: - h dfgd

import UIKit

class FirstController: UIViewController {
    //    pragma mark - ===== 新手指引 =====
    
    let data = [
//        ["name":"发物流", "icon":"","url":"http://"],
//        ["name":"发快递", "icon":"","url":"http://"],
        ["name":"查物流", "icon":"","url":"http://"],
//        ["name":"地址薄", "icon":"","url":"http://"],
        ["name":"查快递", "icon":"","url":"http://"],
        ["name":"查公司", "icon":"","url":"http://"],
        ["name":"查网点", "icon":"","url":"http://"],
//        ["name":"车定位", "icon":"","url":"http://"]
    ]
    
    let data1 = [["name":"顺丰", "icon":"shunfeng","url":"http://"],
                 ["name":"中通", "icon":"zhongtong","url":"http://"],
                 ["name":"申通", "icon":"shentong","url":"http://"],
                 ["name":"圆通", "icon":"yuantong","url":"http://"],
                 ["name":"韵达", "icon":"yunda","url":"http://"],
                 ["name":"极兔", "icon":"jtexpress","url":"http://"],
                 ["name":"德邦", "icon":"debangwuliu","url":"http://"],
                 ["name":"中邮", "icon":"zhongyou","url":"http://"]]
    
    let data2 = [["name":"安能", "icon":"annengwuliu","url":"http://"],
                 ["name":"天地华宇", "icon":"tdhy","url":"http://"],
                 ["name":"佳吉", "icon":"jiaji","url":"http://"],
                 ["name":"大田", "icon":"datian","url":"http://"],
                 ["name":"佳怡", "icon":"jiayi","url":"http://"],
                 ["name":"盛辉", "icon":"shenghui","url":"http://"],
                 ["name":"长通", "icon":"changtong","url":"http://"],
                 ["name":"壹米滴答", "icon":"yimidida","url":"http://"]]
    
    
    var datas = [Array<Any>]()
    let width = UIScreen.main.bounds.width
    let heightFooter = 40.0
    let heightHeader = 40.0
    
    let layout1 = UICollectionViewFlowLayout()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.edgesForExtendedLayout = .init()
        view.backgroundColor = .white
//self.navigationItem.title = "查货宝1"
        self.title = "查货宝"
        datas.append(data)
        datas.append(data1)
        datas.append(data2)
        self.leftItem()
        
        
        layout1.minimumLineSpacing = 10
        layout1.minimumInteritemSpacing = 10
        layout1.itemSize = CGSize(width: 100, height: 100)
        
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout1)
        view.addSubview(collectionView)
        collectionView.backgroundColor = .white
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        collectionView.delegate = self
        collectionView.dataSource = self
        
        collectionView.register(JSCollectionButtonCell.self, forCellWithReuseIdentifier: "JSCollectionButtonCell")
        collectionView.register(JSCollectionBtnCell.self, forCellWithReuseIdentifier: "JSCollectionBtnCell")
        collectionView.register(headerView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "UICollectionReusableView")
        
        // Do any additional setup after loading the view.
        
    }
    
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destination.
     // Pass the selected object to the new view controller.
     }
     */
    
}

// MARK: - UICollectionViewDataSource, UICollectionViewDelegate
extension FirstController: UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    
    // MARK: header/footer的视图
    // 返回Header的宽高
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize
    {
        return CGSize(width: UIScreen.main.bounds.width, height: 35)
        
    }
    // 返回HeaderView的宽高
    func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {
        if elementKind == UICollectionView.elementKindSectionHeader  {
            if indexPath.section==0 {
                view.frame = CGRect.zero
            }else
            {
                view.frame = CGRect(origin: view.frame.origin, size: CGSize(width: width, height: 33))
            }
        }else
        {
            view.frame = CGRect.zero
        }
    }
    //设置 区头和区尾
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        if kind == UICollectionView.elementKindSectionHeader  {
            let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "UICollectionReusableView", for: indexPath) as! headerView
            header.backgroundColor = .white
            
            if indexPath.section==1 {
                header.label.text = "常用快递"
            }else
            {
                header.label.text = "常用物流"
            }
            return header
        }
        else
        {
            return UICollectionReusableView()
        }
        
    }
    
    
    
    /**
     返回有几个分组
     */
    func numberOfSections(in collectionView: UICollectionView) -> Int
    {
        return datas.count
    }
    
    /**
     返回每个分组有几个数据
     */
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        let data = datas[section]
        return data.count
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let dict = datas[indexPath.section][indexPath.row] as! Dictionary<String, Any>
        
        if indexPath.section==0 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "JSCollectionBtnCell", for: indexPath) as! JSCollectionBtnCell
            cell.titleLabel.text = (dict["name"] as! String)
            
            return cell
        }
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "JSCollectionButtonCell", for: indexPath) as! JSCollectionButtonCell
        
        cell.imageView.image = UIImage(named: dict["icon"] as! String)
        cell.titleLabel.text = (dict["name"] as! String)
        
        
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let width1 = (UIScreen.main.bounds.width-layout1.minimumInteritemSpacing*3)/4.0
        if indexPath.section==0 {
            
            return CGSize(width: width1, height: 45)
        }else
        {
            return CGSize(width: width1, height: width1)
        }
    }
    
    // MARK: 点击事件
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let text = String(format: ("你点击了第 %d-%d 个cell"), arguments: [indexPath.section , indexPath.row ])
        print(text)
//        navigationController?.pushViewController(JSWULIUController(), animated: true)
        
        if indexPath.section==0 && indexPath.row==0 {
            tabBarController?.selectedIndex = 1
        }
        if indexPath.section==0 && indexPath.row==1 {
            tabBarController?.selectedIndex = 2
        }
       
    }
    
    
    
}
